package org.zjx.route;

import org.zjx.common.entity.Address;
import org.zjx.common.route.FlyingFlagsRoute;
import org.zjx.common.route.IRoute;

import java.util.ArrayList;
import java.util.List;

public class RouteManager {
    private static final class InstanceHolder {
        static final RouteManager instance = new RouteManager();
    }

    public static RouteManager getInstance() {
        return InstanceHolder.instance;
    }


    public Address startAddress;
    public Address endAddress;
    public List<IRoute> routes = new ArrayList<>();


    public RouteManager() {
        routes.addAll(addWayPoint());
    }

    public List<IRoute> findPath(Address startAddress, Address endAddress, boolean haveFlyingCharms, List<FlyingFlagsRoute.FlyingFlagsPoint> flyingFlagsPoints) {
        List<IRoute> nativeRoute = new ArrayList<>(routes);

        List<IRoute> flyingCharms = flyingCharms(startAddress);
        List<IRoute> flyingFlags = flyingFlags(startAddress, flyingFlagsPoints);
        List<IRoute> flyingCharmsToOther = flyingCharmsToOther(flyingCharms);
        List<IRoute> flyingFlagsToOther = flyingFlagsToOther(flyingFlags);

        if (haveFlyingCharms) nativeRoute.addAll(flyingCharms);
        nativeRoute.addAll(flyingFlags);
        nativeRoute.addAll(flyingCharmsToOther);
        nativeRoute.addAll(flyingFlagsToOther);

        nativeRoute.addAll(RouteUtil.getStartEndRoutes(startAddress, endAddress, nativeRoute));

        // 精确去重：基于起点和终点的坐标+城市进行判断
        List<IRoute> deduplicated = RouteUtil.deduplicateRoutes(nativeRoute);

        DijkstraPathFinder dijkstraPathFinder = new DijkstraPathFinder();
        return dijkstraPathFinder.findPath(deduplicated, startAddress, endAddress);
    }

    //添加传送点之间的路径
    private List<IRoute> addWayPoint() {
        List<IRoute> newRoutes = new ArrayList<>();
        List<IRoute> waypointRoutes = RouteUtil.wayPoint();
        List<IRoute> sameCityRoutes = RouteUtil.connectRoutesWithinSameCity(waypointRoutes, waypointRoutes);  // 传送点之间的 起始/终点 组合
        sameCityRoutes = RouteUtil.deduplicateRoutes(sameCityRoutes); //去重
        newRoutes.addAll(waypointRoutes);
        newRoutes.addAll(sameCityRoutes);
        return newRoutes;
    }

    //飞行符传送路径
    private List<IRoute> flyingCharms(Address startAddress) {
        return RouteUtil.flyingCharms(startAddress);
    }

    //飞行旗传送路径
    private List<IRoute> flyingFlags(Address startAddress, List<FlyingFlagsRoute.FlyingFlagsPoint> flyingFlagsPoints) {
        return RouteUtil.flyingFlags(startAddress, flyingFlagsPoints);
    }

    //飞行符传送后 的同城路径
    private List<IRoute> flyingCharmsToOther(List<IRoute> flyingCharmsRoutes) {
        List<IRoute> newRoutes = RouteUtil.connectRoutesWithinSameCity(flyingCharmsRoutes, routes);  // 传送点之间的 起始/终点 组合
        newRoutes = RouteUtil.deduplicateRoutes(newRoutes); //去重
        return newRoutes;
    }

    //飞行旗传送后 的同城路径
    private List<IRoute> flyingFlagsToOther(List<IRoute> flyingFlagsRoutes) {
        List<IRoute> newRoutes = RouteUtil.connectRoutesWithinSameCity(flyingFlagsRoutes, routes);  // 传送点之间的 起始/终点 组合
        newRoutes = RouteUtil.deduplicateRoutes(newRoutes); //去重
        return newRoutes;
    }

}
